#!/bin/bash

usage="Usage: ./fioperf.sh iocache iodepth blocksz jobs log_filename"

if [ $# -ne 5 ]; then
	echo $usage
else
iocache=$1
iodirect=""
iodepth=$2
blocksz=$3
jobs=$4
log=$5
echo ">>>>>>>>>> file random read/writes >>>>>>>>>>> " >> $log
if [[ $iocache == "0" ]]; then
	echo "Use host cache: [Yes]" >> $log
	iodirect=1
elif [[ $iocache == "1" ]]; then
	echo "Use host cache: [No]" >> $log
	iodirect=0
fi
echo "I/O depth: [$iodepth]" >> $log
echo "Block size: [$blocksz] Bytes" >> $log
echo "Concurrent jobs: [$jobs]" >> $log
fio --filename=/data/file --size=5GB --direct=$iodirect --rw=randrw --bs=$blocksz --ioengine=libaio --iodepth=$iodepth --runtime=120 --numjobs=$jobs --time_based --group_reporting --name=iops-file-randrw --eta-newline=1 >> $log
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $log

sleep 10

echo ">>>>>>>>>>> raw device random read/writes >>>>>>>>>>>" >> $log
if [[ $iocache == "0" ]]; then
	echo "Use host cache: [Yes]" >> $log
	iodirect=1
elif [[ $iocache == "1" ]]; then
	echo "Use host cache: [No]" >> $log
	iodirect=0
fi
echo "I/O depth: [$iodepth]" >> $log
echo "Block size: [$blocksz] Bytes" >> $log
echo "Concurrent jobs: [$jobs]" >> $log
fio --filename=/dev/sdc --direct=$iodirect --rw=randrw --bs=$blocksz --ioengine=libaio --iodepth=$iodepth --runtime=120 --numjobs=$jobs --time_based --group_reporting --name=iops-raw-randrw --eta-newline=1 >> $log
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $log

sleep 10

echo ">>>>>>>>>>> Latency Performance: raw device random read/writes >>>>>>>>>>>" >> $log
if [[ $iocache == "0" ]]; then
	echo "Use host cache: [Yes]" >> $log
	iodirect=1
elif [[ $iocache == "1" ]]; then
	echo "Use host cache: [No]" >> $log
	iodirect=0
fi
echo "I/O depth: [$iodepth]" >> $log
echo "Block size: [$blocksz] Bytes" >> $log
echo "Concurrent jobs: [1]" >> $log
fio --filename=/dev/sdc --direct=$iodirect --rw=randrw --bs=$blocksz --ioengine=libaio --iodepth=$iodepth --numjobs=1 --time_based --group_reporting --name=latency-raw-randrw --runtime=120 --eta-newline=1 >> $log
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $log

fi